package org.youchan.aco.ui;

import java.awt.Image;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import javax.swing.SwingUtilities;

import org.xml.sax.SAXException;
import org.youchan.aco.AcoPreferences;
import org.youchan.mashedpotato.framework.ui.Action;
import org.youchan.mashedpotato.framework.ui.ActionEvent;
import org.youchan.mashedpotato.framework.ui.DefineUI;
import org.youchan.mashedpotato.framework.ui.IUI;
import org.youchan.mashedpotato.framework.ui.UIBuilder;
import org.youchan.mashedpotato.framework.ui.UILoader;
import org.youchan.util.ImageCache;
import org.youchan.util.XMLResourceBundle;

public class UIManager {
	private static UIManager self_;
	
	private Properties mUIResources;
	private MainWindow mMainWindow;
	private ImageCache mImageCache;
	IUI settingUI;

	private UIManager() {
		mImageCache = new ImageCache();
		try {
			mUIResources = XMLResourceBundle.getResource("org.youchan.aco.UIResources");
		} catch (InvalidPropertiesFormatException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
        try {
            UILoader loader = new UILoader();
            DefineUI defUI = loader.load(this.getClass().getResourceAsStream("/org/youchan/aco/ui/setting.ui"));
            UIBuilder builder = new UIBuilder();
            settingUI = builder.createUI(defUI);
            String id = AcoPreferences.getInstance().get(AcoPreferences.Key.TWITTER_ID);
            String password = AcoPreferences.getInstance().get(AcoPreferences.Key.TWITTER_PASSWORD);
            settingUI.getUIParts("id_text").setProperty("text", id);
            settingUI.getUIParts("pw_text").setProperty("text", password);
            settingUI.addAction("ok_button.pushed", new Action() {
				public void invoke(ActionEvent event) {
					String id = settingUI.getUIParts("id_text").getProperty("text");
					String password = settingUI.getUIParts("pw_text").getProperty("text");
					AcoPreferences.getInstance().set(AcoPreferences.Key.TWITTER_ID, id);
					AcoPreferences.getInstance().set(AcoPreferences.Key.TWITTER_PASSWORD, password);
					settingUI.getDefaultWindow().close();
				}
            });
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static UIManager getInstance() {
		if (self_ == null) {
			self_ = new UIManager();
		}
		return self_;
	}
	
	public void start() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				mMainWindow = new MainWindow();
				mMainWindow.setVisible(true);
			}
		});
	}
	
	public static String getResource(String key) {
		return (String)getInstance().mUIResources.get(key);
	}
	
	public MainWindow getMainWindow() {
		return mMainWindow;
	}
	
	public Image getImage(String url) throws IOException {
		return mImageCache.getImage(url);
	}
	
	public void showSettingWindow() {
		settingUI.getDefaultWindow().show();
	}

	public void exit() {
		mMainWindow.setVisible(false);
		System.exit(0);
	}
}
